package com.zhhs.galaxyott.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.adapters.HotMovieGridAdapter;
import com.zhhs.galaxyott.adapters.ICallback;
import com.zhhs.galaxyott.beans.HotMoviesBean;
import com.zhhs.galaxyott.http.api.RequestMaker;
import com.zhhs.galaxyott.httpVolley.base.ResponseListener;
import com.zhhs.galaxyott.httpVolley.response.HotMoviesResponse;
import com.zhhs.galaxyott.utils.Logs;

public class HotMovieActivity extends Activity {

    private TextView watch_number, name, ename, category_name, director, actor;

    private ImageView type_img;
    private ImageView special_img;
    private GridView lvMovie;
    HotMovieGridAdapter adapter;
    List<HotMoviesBean> listData = new ArrayList<HotMoviesBean>();

    private int specialId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_movie);

        specialId = getIntent().getIntExtra("specailId", 1);

        type_img = (ImageView) findViewById(R.id.type_img);
        if (specialId == 1) {
            type_img.setBackgroundResource(R.drawable.hotmovie_img);
        } else {
            type_img.setBackgroundResource(R.drawable.more_topic);
        }

        special_img = (ImageView) findViewById(R.id.special_img);

        lvMovie = (GridView) findViewById(R.id.grid_movie);

        loadData();

    }

    ICallback mCallback = new ICallback() {

        @Override
        public void setText(int pos) {

            setTextView(pos);
        }
    };

    private void loadData() {
        int pageNo = 1;
        int pageRow = 100;

        RequestMaker.getHotMovie(this, specialId, pageNo, pageRow, new ResponseListener<HotMoviesResponse>() {

            @Override
            public void onResponse(HotMoviesResponse result) {

                if (result.Beans == null || result.Beans.size() == 0) {
                    return;
                }

                Logs.logD("DEBUG_DT",
                        "THIS getHotMovie--->" + result.msg + result.Beans.size() + "name" + result.getImageUrl());
                if (result.errCode == 0) {
                    listData = result.Beans;
                }
                Glide.with(HotMovieActivity.this).load(result.getImageUrl()).crossFade().into(special_img);

                adapter = new HotMovieGridAdapter(HotMovieActivity.this, listData, mCallback);
                setGridView();

                initView();

            }

            @Override
            public void onError(VolleyError error) {
                Logs.logD("DEBUG_DT", "THIS getHotMovie--->ERR");
            }
        });
    }

    public void initView() {

        watch_number = (TextView) findViewById(R.id.watch_number);
        name = (TextView) findViewById(R.id.name);
        ename = (TextView) findViewById(R.id.ename);
        category_name = (TextView) findViewById(R.id.category_name);
        director = (TextView) findViewById(R.id.director);
        actor = (TextView) findViewById(R.id.actor);

    }

    public void setTextView(int pos) {

        HotMoviesBean bean = listData.get(pos);

        watch_number.setText(bean.getWatchNum() + "观看");
        name.setText(bean.getName());
        ename.setText(bean.getEname());
        category_name.setText("类型：" + bean.getCategoryName());
        director.setText("导演：" + bean.getDirector());
        actor.setText("主演：" + bean.getActor());
    }

    /** 设置GirdView参数，绑定数据 */
    private void setGridView() {
        int length = 200;
        int size = adapter.getCount();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        Logs.logD("DEBUG_d", "THIS IS density-->" + density);

        int gridviewWidth = (int) (size * (length + 0) * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridviewWidth,
                LinearLayout.LayoutParams.MATCH_PARENT);
        lvMovie.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        lvMovie.setColumnWidth(itemWidth); // 设置列表项宽

        // 实现单行显示需要在代码中处理，如果使用xml中的“anto_fit”并没有用
        lvMovie.setNumColumns(size); // 设置列数量=列表集合数
        lvMovie.setAdapter(adapter);

    }
}
